package cn.controller;

import cn.basic.BaseController;
import cn.dao.ApplyDAO;
import cn.factory.DAOFactory;
import cn.model.TApply;
import cn.model.VApply;
import cn.model.VUserRole;
import cn.util.Expression;
import cn.util.Toolsutil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 申请信息数据交互
 * @author WZG
 * @version 2020-2-30
 */
@Controller
@RequestMapping(value="/apply")
public class ApplyController extends BaseController {


    @RequestMapping(value="/getapplylistByPage")//映射路径,拦截post请求
    public void getApplyList(
            String equipmentName,
            Integer auditstatus,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        Expression exp = new Expression();
        if(equipmentName != null && !equipmentName.equals("")) {
            exp.andEqu("equipmentName", equipmentName, String.class);
        }
        if (auditstatus!=null && !auditstatus.equals("")){
            exp.andEqu("auditstatus",auditstatus,Integer.class);
        }
        ApplyDAO apdao = DAOFactory.getApplyDAO();
        String wherecondition = exp.toString();
        List<VApply> list = apdao.getApplyListByPage(wherecondition,page, limit);
        int size = apdao.getApplyAmount(wherecondition);
        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }

    @RequestMapping(value="/getapplylistByStatus")//映射路径,拦截post请求
    public void getApplyListBystatus(
            String equipmentName,
            String realname,
            Integer page,
            Integer limit,//一页多少条

            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        Expression exp = new Expression();
        if(equipmentName != null && !equipmentName.equals("")){
            exp.andEqu("equipmentName", equipmentName, String.class);}
        if (realname !=null && !realname.equals("")){
            exp.andEqu("realname",realname,String.class);
        }
        String wherecondition = exp.toString();
        ApplyDAO apdao = DAOFactory.getApplyDAO();
        List<VApply> list = apdao.getVApplyByStatus(1,page, limit);
        int size = apdao.getApplyAmount(wherecondition);
        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + dt.count;
            dt.data = list;
            dt.count = size;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }


    @RequestMapping(value="/getapplylistByUser")//映射路径,拦截post请求
    public void getApplyListByUser(
            Integer page,
            Integer limit,//一页多少
            HttpServletRequest request,
            HttpServletResponse response,
            Model model){

       VUserRole user = (VUserRole)session.getAttribute("loginuser");

        String userid  = user.getUserid();

        Expression exp = new Expression();
        ApplyDAO apdao = DAOFactory.getApplyDAO();
        List<VApply> list = apdao.getVApplyByUser(userid,page, limit);
        int size = apdao.getApplyAllAmount(userid);


        if (list!=null) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "查询成功，共查出" + list.size()+ "条记录";
            dt.count=size;
            dt.data = list;
        }else{
            dt.code = dt.FLAG_FAIL;
            dt.msg = "查询失败";
        }
        closeout();
    }




    @RequestMapping(value="/addApply")
    public void AddApply(
            String equipmentName,
            String reason,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        System.out.println(equipmentName);
        System.out.println(reason);


        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        TApply apply = new TApply();
        apply.setEquipmentName(equipmentName);
        apply.setApplicationTime(Toolsutil.getdatetimes());
        apply.setReason(reason);
        apply.setUserid(user.getUserid());
        apply.setAuditstatus(1);
        ApplyDAO apdao = DAOFactory.getApplyDAO();

        System.out.println(equipmentName);
        System.out.println(reason);
        System.out.println(user.getUserid());

        if (apdao.addApply(apply) > 0) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "添加成功";
        } else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "添加失败";
        }
        closeout();
    }

    /*
     * 实现删除
     * @param userid 用户名
     * @throws IOException
     */
    @RequestMapping(value="/deleteapply")
    public void deleteapply(
            String applyid,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException{
        ApplyDAO apdao = DAOFactory.getApplyDAO();
        if (applyid !=null && !applyid.equals("")){
            apdao.deleteApply(Integer.parseInt(applyid));
            dt.code = dt.FLAG_SUCC;
            dt.msg = "删除成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "删除失败 ";
        }
        closeout();
    }

    @RequestMapping(value="/modifyapply")
    public void modifyApply(
            Integer applyid,
            String equipmentName,
            String reason,
            HttpServletRequest request,
            HttpServletResponse response,
            Model model) throws IOException{

        VUserRole user = (VUserRole)session.getAttribute("loginuser");
        TApply apply = new TApply();
        apply.setApplyid(applyid);
        apply.setEquipmentName(equipmentName);
        apply.setReason(reason);
        apply.setUserid(user.getUserid());
        ApplyDAO apdao = DAOFactory.getApplyDAO();
        if (apdao.modifyApply(apply)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }
    @RequestMapping(value="/modifyaudit")
    public void modifyApplyById(
            Integer applyid) throws IOException{

        ApplyDAO apdao = DAOFactory.getApplyDAO();

        if (apdao.modifyApplyById(applyid,2)) {
            dt.code = dt.FLAG_SUCC;
            dt.msg = "修改成功";
        }else {
            dt.code = dt.FLAG_FAIL;
            dt.msg = "修改失败";
        }
        closeout();
    }
}
